﻿using Mysoft.Map.Extensions.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mysoft.QuickCode.ServiceProvider
{
    public class ServiceObjectConvert
    {
        /// <summary>
        /// 从MysoftHttpRequest请求中获取强类型请求参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="req"></param>
        /// <returns></returns>
        public static T GetObject<T>(ServiceRequest req)
        {
            T data = JsonHelper.JsonDeserialize<T>(req.Data.ToString());
            //此处可以进行实体验证，并使用Try的形式抛出。
            return data;
        }

        public static ServiceResponse ToResponse(object data)
        {
            return new ServiceResponse()
            {
                Data = JsonHelper.JsonSerialize(data)
            };
        }
    }

}